<?php
namespace app\index\controller;

use app\common\model\ZhMonks as ZhMonksModel;
use app\common\model\ZhCate as ZhCateModel;

/**
 * 高僧大德控制器
 * Class Zhmonks
 * @package app\index\controller
 */
class Zhmonks extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $user = $this->user;
        $model = new ZhMonksModel;
        $cmodel = new ZhCateModel;
        $this->getPageAndSize($param);
        $map = [];
        $map1 = [];
        try {
            $class = config('oneclass');
            $map1['cid'] = 4;
            $map1['comid'] = $user['comid'];
            $map1['states'] = ['neq',3];
            $cate = $cmodel->getAllData($map1,'id','id,cname');
            $cate = array_merge($class[3]['cate'],$cate);
            if(!empty($param['fahao'])){
                $map['fahao'] = $param['fahao'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            if(!empty($param['cate'])){
                $map['cate'] = $param['cate'];
            }
            $map['states'] = ['neq',3];
            $map['comid'] = $user['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,headimg,fahao,cate,content,states,intro,create_time');
            foreach($lists as &$v){
                foreach($cate as &$v1){
                    if($v1['id'] == $v['cate']){
                        $v['cname'] = $v1['cname'];
                    }
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        writelog($user['mid'],$user['comid'],'查看高僧大德列表');
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  添加]
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new ZhMonksModel;
        $user = $this->user;
        try{
            $param['comid'] = $user['comid'];
            $model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'添加成功');
    }
    /**
     * [edit  banner编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ZhMonksModel;
        $user = $this->user;
        try{
            $banner = $model->findData($param['id'],'id','comid');
            if($banner['comid'] != $user['comid']){
                return admin_json(-1,'编辑失败');
            }
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'编辑成功');
    }
    /**
     * [del  banner删除]
     * @return json
     * @author [C。]
     */
    public function del()
    {
        $param = input('param.');
        $model = new ZhMonksModel;
        $user = $this->user;
        try{
            $banner = $model->findData($param['id'],'id','comid');
            if($banner['comid'] != $user['comid']){
                return admin_json(-1,'删除失败');
            }
            $model->editData(['id'=>$param['id'],'states'=>3]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1,$e->getMessage());
        }
        return admin_json(1,'删除成功');
    }
}